﻿using Grpc.Core;
using GRPCClass;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;

namespace SdtpsLibrary
{
    public class GRPCServer
    {
        public static int Port = 9007;
        public static string Ipadd = "";
        /// <summary>
        /// 启动服务
        /// </summary>
        public static string StartServer()
        {
            Port = SdtpsBase.Util.StrToInt(ConfigurationManager.AppSettings["nodeport"].ToString());
            Server server = new Server
            {
                Services = { gRPC.BindService(new gRPCImpl()) },
                Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };
            string ipadd = "";
            IPAddress[] ipArray;
            ipArray = Dns.GetHostAddresses(Dns.GetHostName());
            for (int i = 0; i < ipArray.Length; i++)
            {
                if (!ipArray[i].IsIPv6LinkLocal)
                {
                    server.Ports.Add(new ServerPort(ipArray[i].ToString(), Port, ServerCredentials.Insecure));
                    ipadd = ipArray[i].ToString();
                }
            }
            server.Start();
            Ipadd = ipadd;
            return ipadd;
        }

    }
}
